package com.fy56.platform.logistics.restful.api.controller.wx;

import com.alibaba.fastjson.JSON;
import com.fy56.platform.logistics.common.ResultModel;
import com.fy56.platform.logistics.common.ResultStatus;
import com.fy56.platform.logistics.model.WithdrawMoney;
import com.fy56.platform.logistics.service.wx.WithdrawMoneyService;
import com.fy56.platform.logistics.utils.StringUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author  by Edwin Yang
 */
@Api(value = "WithdrawMoney", description = "掌上取款业务", produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/withdraw")
public class WithdrawMoneyController {

    @Autowired
    private WithdrawMoneyService withdrawMoneyService;

    private static Logger log = Logger.getLogger(WithdrawMoneyController.class);

    /**
     * 验证是否有重复的票据
     *
     * @param withdrawMoneyLst
     * @return
     */
    private String checkData(List<WithdrawMoney> withdrawMoneyLst){
        Map<String, Integer> map = Maps.newHashMap();
        for(WithdrawMoney model : withdrawMoneyLst) {
            if (map.containsKey(model.getOrderNo())) {
                return model.getOrderNo();
            } else {
                map.put(model.getOrderNo(), 1);
            }
        }
        return null;
    }



    /**
     * 验证取款票据信息;
     * @date 2018/2/26 0026.
     * @para data:需要进行验证的票号List(JsonList)
     */
    @RequestMapping(value = "/validateWithdraw", method = RequestMethod.POST)
    @ApiOperation(value = "验证取款票据信息")
    public ResponseEntity validateWithdrawMoney(@RequestParam String data){

        Map<String,Object> param = JSON.parseObject(data);
        String tenant = param.get("tenant").toString();
        String mobile = param.get("mobile").toString();
        List<WithdrawMoney> withdrawMoneyZBList = JSON.parseArray(param.get("order").toString(),WithdrawMoney.class);
        //验证是否有重复的票据
        String isMoreThanOne = checkData(withdrawMoneyZBList);
        if(isMoreThanOne != null){
            return new ResponseEntity(new ResultModel(200,"票号：" + isMoreThanOne + "重复", Lists.newArrayList()), HttpStatus.OK);
        }
        log.info("企业：" + tenant + "；手机号" + mobile);
        try {
            return new ResponseEntity(withdrawMoneyService.validateOrder(tenant,withdrawMoneyZBList,mobile), HttpStatus.OK);
        } catch (Exception e) {
            log.error("验证取款票据信息",e);
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, "验证取款票据信息"), HttpStatus.OK);
        }
    }

    /**
     * 提审取款记录;
     * @date 2018/2/27 0026.
     * @para data:需要进行提审的票号List(JsonList)
     */
    @RequestMapping(value = "/addWithdraw", method = RequestMethod.POST)
    @ApiOperation(value = "提审取款记录")
    public ResponseEntity addWithdrawMoney(@RequestParam String data){
        log.info("提审取款记录---参数：" + data);
        String accountName = JSON.parseObject(data).getString("accountName");
        String bankAccount =  JSON.parseObject(data).getString("bankAccount");
        String bankName =  JSON.parseObject(data).getString("bankName");
        String mobile =  JSON.parseObject(data).getString("mobile");
        String idCard =  JSON.parseObject(data).getString("idCard");
        String vipNo =  JSON.parseObject(data).getString("vipNo");
        String code = "";
        List<WithdrawMoney> withdrawMoneyZBList = JSON.parseObject(data).getJSONArray("order").toJavaList(WithdrawMoney.class);
        for(int i = 0; i < withdrawMoneyZBList.size(); i++){
            withdrawMoneyZBList.get(i).setBankAccount(bankAccount);
            withdrawMoneyZBList.get(i).setVipNo(StringUtil.isEmpty(vipNo) ? mobile : vipNo);
            withdrawMoneyZBList.get(i).setBankName(bankName);
            withdrawMoneyZBList.get(i).setAccountName(accountName);
            withdrawMoneyZBList.get(i).setTelphone(mobile);
            withdrawMoneyZBList.get(i).setIdCard(idCard);
            code =  withdrawMoneyZBList.get(i).getFinancialCode();
        }
        try {
            return new ResponseEntity(withdrawMoneyService.addWithdraw(withdrawMoneyZBList,code), HttpStatus.OK);
        } catch (Exception e) {
            log.error("提审取款记录",e);
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, "提审取款记录"), HttpStatus.OK);
        }
    }

    /**
     * 查询本地取款记录;
     * @date 2018/3/14 0026.
     */
    @RequestMapping(value = "/findWithdrawLocal", method = RequestMethod.POST)
    @ApiOperation(value = "查询本地取款记录")
    public ResponseEntity findWithdrawLocal(@RequestParam String mobile,@RequestParam String pageCount,@RequestParam String pageIndex){
        log.info("查询本地取款记录：手机号" + mobile);
        try {
            return new ResponseEntity(ResultModel.ok(withdrawMoneyService.findWithdrawLocal(mobile,Integer.valueOf(pageCount),Integer.valueOf(pageIndex))), HttpStatus.OK);
        } catch (Exception e) {
            log.error("查询本地取款记录失败",e);
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, "查询本地取款记录失败"), HttpStatus.OK);
        }
    }



    /**
     * 查询取款记录明细;
     * @date 2018/2/27 0026.
     */
    @RequestMapping(value = "/findWithdrawDetail", method = RequestMethod.POST)
    @ApiOperation(value = "查询取款记录明细")
    public ResponseEntity findWithdrawMoneyDetail(@RequestParam String withdrawNo,@RequestParam String mobile){
        log.info("查询取款记录明细：取款编号：" + withdrawNo + "；手机号：" + mobile);
        try {
            return new ResponseEntity(ResultModel.ok(withdrawMoneyService.findWithdrawMoneyDetail(withdrawNo,mobile)), HttpStatus.OK);
        } catch (Exception e) {
            log.error("查询取款记录明细失败",e);
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, "查询取款记录明细失败"), HttpStatus.OK);
        }
    }

    /**
     * 删除取款记录明细;
     * @date 2018/2/27 0026.
     */
    @RequestMapping(value = "/delWithdrawDetail", method = RequestMethod.POST)
    @ApiOperation(value = "删除取款记录明细")
    public ResponseEntity delWithdrawDetail(@RequestParam String withdrawNo,@RequestParam String orderNo){
        log.info("删除取款记录明细：取款编号：" + withdrawNo + "；票号：" + orderNo);
        try {
            return new ResponseEntity(ResultModel.ok(withdrawMoneyService.delWithdrawDetail(withdrawNo,orderNo)), HttpStatus.OK);
        } catch (Exception e) {
            log.error("删除取款记录明细失败",e);
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, "删除取款记录明细失败"), HttpStatus.OK);
        }
    }


    /**
     *  查询代收记录
     * @param code 金融代码
     * @param telphone 电话号码
     * @param state 状态
     * @param giveStartDate 发放开始日期
     * @param giveEndDate  发放结束日期
     * @param startDate 开票开始日期
     * @param endDate 开票结束日期
     * @return
     */
    @RequestMapping(value = "/searchDCInfo", method = RequestMethod.POST)
    @ApiOperation(value = "查询代收记录")
    public ResponseEntity searchDeliveryCollectionInfo(@RequestParam String code,
                                                       @RequestParam  String telphone,
                                                       @RequestParam  String state,
                                                       @RequestParam(required = false)  String giveStartDate,
                                                       @RequestParam(required = false)  String giveEndDate,
                                                       @RequestParam(required = false)  String startDate,
                                                       @RequestParam(required = false)  String endDate){
        log.info("查询代收记录：参数：手机号["+telphone+"];提现状态["+state+"];开始日期["+startDate+"];结束日期["+endDate+"]");
        try {
            return new ResponseEntity(withdrawMoneyService.searchDeliveryCollectionInfo(code,telphone,state,giveStartDate,giveEndDate,startDate,endDate), HttpStatus.OK);
        } catch (Exception e) {
            log.error("查询代收记录失败",e);
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, "查询代收记录失败"), HttpStatus.OK);
        }
    }

}
